package cn.gson.springboot.model.service.saleservice;

import cn.gson.springboot.model.dao.saledao.*;
import cn.gson.springboot.model.mapper.salemapper.CtschemeMapper;
import cn.gson.springboot.model.mapper.salemapper.OrderMapper;
import cn.gson.springboot.model.pjos.*;
import cn.gson.springboot.model.vo.salesvo.RefundinfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class CtschemesService {
    @Autowired
    CtschemesDao ctschemesDao;
    @Autowired
    OrderinvoiceDao orderinvoiceDao;
    @Autowired
    DeliveryplanDao deliveryplanDao;
    @Autowired
    DeliverydetailsDao deliverydetailsDao;
    @Autowired
    ExchangeDao exchangeDao;
    @Autowired
    ExchangeDetaDao exchangeDetaDao;
    @Autowired
    CtschemeMapper ctschemeMapper;
    @Autowired
    ContractDao contractDao;
    @Autowired
    OrderInfoDao orderInfoDao;
    @Autowired
    OueboundorderDao oueboundorderDao;
    @Autowired
    ZproductsideDao zproductsideDao;

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ReturnorderDao returnorderDao;
    @Autowired
    ReturnorderdetailsDao returnorderdetailsDao;
    @Autowired
    ZfhuodanDao zfhuodanDao;
    @Autowired
    RefundinfoDao refundinfoDao;

    public Integer selectMaxDeliveryId(Integer orderId){
        Integer integer = ctschemeMapper.selectMaxDeliveryId(orderId);
        return integer;
    }

    public void saveRefundInfo(RefundinfoEntity refundinfoEntity){
        refundinfoDao.save(refundinfoEntity);
    }

    public Integer saveCtschems(Ctschemes ctschemes){
        Ctschemes save = ctschemesDao.save(ctschemes);
        if(save!=null){
            return 200;
        }else {
            return 500;
        }
    }
    
    
    public Integer saveOrderinvoce(Orderinvoice orderinvoice){
        Orderinvoice save = orderinvoiceDao.save(orderinvoice);
        if(save!=null){
            return 200;
        }else {
            return 500;
        }
    }

    public Integer saveDeliveryplan(Deliveryplan deliveryplan){
        Deliveryplan save = deliveryplanDao.save(deliveryplan);
        if(save!=null){
            return 200;
        }else {
            return 500;
        }
    }

    public void saveDeliverydetails(List<Deliverydetails> deliverydetailsList){
        deliverydetailsDao.saveAll(deliverydetailsList);

    }

    public void saveExchang(Exchange exchange){
         Boolean falge=true;
        java.util.Date utilDate = new java.util.Date();//util.Date
        //util.Date转sql.Date
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        exchange.setExcNewtime(sqlDate);

        exchange.setExcState(0);
        String name = selectMaxExchangeName();
        exchange.setEcName(name);
        exchangeDao.save(exchange);
        //改变发货单状态不能再退换货
        fhuodState((Integer.parseInt(exchange.getExcOrder())),falge);

    }

    public void fhuodState(Integer id,boolean falge){
        ZfhuodanEntity zfhuodanEntity = zfhuodanDao.findById(id).get();
        if(falge){
            zfhuodanEntity.setShipmentsState(2);
        }
        if(!falge){
            zfhuodanEntity.setShipmentsState(3);
        }
        zfhuodanDao.save(zfhuodanEntity);
    }

    public void saveExchangdata(List<Exchangedetails> exchangedetails){
        exchangeDetaDao.saveAll(exchangedetails);

    }


    public Integer saveReturnInfo(Returnorder returnorder){
         Boolean falge=false;
        java.util.Date utilDate = new java.util.Date();//util.Date
        //util.Date转sql.Date
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        returnorder.setRetuTata(sqlDate);

        String name = selectMaxReturnName();
        returnorder.setRetuName(name);
        returnorder.setRetuState(0);
        Returnorder save = returnorderDao.save(returnorder);
        //改变发货单状态不能再退换货
        fhuodState((Integer.parseInt(returnorder.getRetuOrder())),false);
        return  save.getRetuId();

    }

    public void saveReturndataInfo(List<Returnorderdetails> list){
        returnorderdetailsDao.saveAll(list);
    }


    /**
     * 交付计划
     * @param orderId
     * @return
     */
    public List<Deliveryplan> deliveryplanList (Integer orderId){
        List<Deliveryplan> deliveryplans = ctschemeMapper.deliveryplanList(orderId);
        for(Deliveryplan info :deliveryplans){
            System.out.println("性格开朗"+info.toString());
        }
        return  deliveryplans;
    };


    /**
     * 回款
     * @param orderId
     * @return
     */
    public List<Ctschemes> ctschemesList(Integer orderId){
        List<Ctschemes> ctschemes = ctschemeMapper.ctschemesInfo(orderId);
        return ctschemes;
    }

    /**
     * 发票
     * @param orderId
     * @return
     */
    public List<Orderinvoice> orderinvoiceList(Integer orderId){
        List<Orderinvoice> orderinvoices = ctschemeMapper.orderinvoiceList(orderId);
        return orderinvoices;
    }


    /**
     * 回款计划回款
     * @param contractId
     * @param hkPlay
     * @param fpstate
     * @param amount
     * @param ctId
     */
    public void changCtschems(Integer contractId,Integer hkPlay,Integer fpstate,Integer amount,Integer ctId,Integer orderId) {

        BigDecimal bigDecimal = new BigDecimal(amount.toString());
        //回款计划
        Ctschemes ctschemes = ctschemesDao.findById(ctId).get();
        //订单
        OrderInfo orderInfo = orderInfoDao.findById(orderId).get();
        BigDecimal bigDecimalt = orderInfo.getReceivableOrder();

        orderInfo.setReceivableOrder(bigDecimal.add(bigDecimalt));
        orderInfoDao.save(orderInfo);
        //回款计划金额与回款金额匹配
        if(ctschemes.getCtAmount()==bigDecimal){
            ctschemes.setCtWay(hkPlay);
            ctschemes.setCtState(1);
            ctschemesDao.save(ctschemes);
        }else{
            ctschemes.setCtAmount(bigDecimal);
            ctschemes.setCtWay(hkPlay);
            ctschemes.setCtState(1);
            ctschemesDao.save(ctschemes);
        }

    }

    public List<Deliveryplan> invoiceInfo(){
        List<Deliveryplan> deliveryplans = ctschemeMapper.invoiceInfo();
        return deliveryplans;
    }

    //交付记录删除
    public void deleteDeliveryInfo(Integer deliveryId){
        deliveryplanDao.deleteById(deliveryId);

    }

    //将交付记录设置成可出库
    public void outboundDeliveryInfo(Integer id ){
        Deliveryplan deliveryplan = deliveryplanDao.findById(id).get();
        deliveryplan.setDeState(1);
        outboundInfo(id);
        deliveryplanDao.save(deliveryplan);
    }

    /**
     * 新增发货单
     * @param id
     */
    public void outboundInfo(Integer id){
        Outboundorder outboundorder = new Outboundorder();
        Date utilDate = new Date();//util.Date
        System.out.println("utilDate : " + utilDate);
        //util.Date转sql.Date
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        Deliveryplan deliveryplan = deliveryplanDao.findById(id).get();

        upProductNumber(id);
        outboundorder.setOutorderId(deliveryplan.getOrderInfoByOrderId().getOrderId());
        outboundorder.setOutTime(sqlDate);
        outboundorder.setOutState(0);
        outboundorder.setOutuserId(1001);
        outboundorder.setDeliveryplanByDeliveryId(deliveryplan);
        oueboundorderDao.save(outboundorder);

    }

    //出库产品数量调整
    public void upProductNumber( Integer deliveryplanid ){

        Deliveryplan deliveryplan = deliveryplanDao.findById(deliveryplanid).get();//交付计划
        List<Deliverydetails> list = deliveryplan.getDeliverydetailsByDeliveryId();//产品

        for(int i=0;i<list.size();i++){
            Integer detaProductid = list.get(i).getDetaProductid();//副标id
            Integer detaNumber = list.get(i).getDetaNumber();//副标出库数量

            //mybaits 查询
            Zproductside zproductside = orderMapper.upData(detaProductid);//仓库产品副标

            Integer newNumber=zproductside.getPsNumber()-detaNumber;//新数量


            orderMapper.upProduct(newNumber,detaProductid);

            Zproductside zproduct = new Zproductside();
            zproduct.setPsId(detaProductid);
            zproduct.setPsNumber(newNumber);
            zproduct.setZproductByProductId(zproductside.getZproductByProductId());

            zproductsideDao.save(zproduct);

        }

    }



    public String selectMaxExchangeName(){
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
        Date date = new Date();//获取当前时间
        String code = ctschemeMapper.selectMaxExchangeName();
        //如果查询出来的数字为空，则为当天第一个
        if(null == code || "".equals(code)){
            code = "H"+sdf.format(date)+"-"+"1";
        }else{
            //数字不为空则加一，
            int zhi = Integer.parseInt(code)+1;
            code = "H"+sdf.format(date)+"-"+zhi+"";
        }
        return code;
    }

    public String selectMaxReturnName(){
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
        Date date = new Date();//获取当前时间
        String code = ctschemeMapper.selectMaxReturnorderName();
        //如果查询出来的数字为空，则为当天第一个
        if(null == code || "".equals(code)){
            code = "H"+sdf.format(date)+"-"+"1";
        }else{
            //数字不为空则加一，
            int zhi = Integer.parseInt(code)+1;
            code = "H"+sdf.format(date)+"-"+zhi+"";
        }
        return code;
    }


    /**
     * 回款列表
     * @return
     */
    public List<Ctschemes> allCtshemsInfo(String searchInfo){
        List<Ctschemes> ctschemes = ctschemeMapper.allCtshemsInfo(searchInfo);
        return ctschemes;
    }


    public List<RefundinfoVo> RefundinfoInfo(String searchInfo){
        List<RefundinfoVo> refundinfoVos = ctschemeMapper.RefundinfoInfo(searchInfo);
        return refundinfoVos;
    }


}
